/* main.c */

/*
    This program demonstrates a simple application of JSON_checker. It reads
    a JSON text from STDIN, producing an error message if the text is rejected.

        % JSON_checker <test/pass1.json
*/

#include <stdlib.h>
#include <stdio.h>
#include "JSON_checker.h"

int main(int argc, char* argv[]) {
/*
    Read STDIN. Exit with a message if the input is not well-formed JSON text.

    jc will contain a JSON_checker with a maximum depth of 20.
*/
    JSON_checker jc = new_JSON_checker(20);
	int errAt = 1, errLine = 1;
	char buf[128] = "ok";
    for (;;) {
        int next_char = getchar();
        if (next_char <= 0) {
            break;
        }
        if (!JSON_checker_char(jc, next_char)) {
			sprintf(buf, "line %d column %d: syntax error\n", errLine, errAt);
            fprintf(stderr, buf);
            exit(1);
        }
		++errAt;
		if (next_char == '\n') {
			errAt = 1;
			++errLine;
		}
    }
    if (!JSON_checker_done(jc)) {
        fprintf(stderr, "line %d column %d: syntax error\n", errLine, errAt);
        exit(1);
    }
}